﻿//
// page onload event
//
document.onreadystatechange = function () {
    if (document.readyState == "complete") {

        FormMessage.initialise();
    }
}



var FormMessage = {
  
    initialise: function () {
        var qs = this.getQueryStringAsJSON();
        if (qs == null)
            return;

        var text = "";

        if (qs.data != "undefined" && qs.data != null && qs.data.text != "undefined" && qs.data.text != null)
            text = qs.data.text;

        var type = null;
        if (qs.data != "undefined" && qs.data != null && qs.data.type != "undefined" && qs.data.type != null)
            type = qs.data.type;

        if (type != null) {
            switch (type) {
                case "info":
                    document.getElementById("uxInfo").style.display = "block";
                    break;
                case "warning":
                    document.getElementById("uxWarning").style.display = "block";
                    break;
                case "alert":
                    document.getElementById("uxAlert").style.display = "block";
                    break;
                case "error":
                    document.getElementById("uxError").style.display = "block";
                    break;
                case "success":
                    document.getElementById("uxSuccess").style.display = "block";
                    break;
            }
        }

        document.getElementById("uxText").innerHTML = text;
    },

      

    getQueryStringAsJSON : function () {
        
        var queryObject = null;
    
        if (location.search != "") {
        
            var attributes = this._parseQueryStringAsJSONString(location.search.substr(1), null);
            if(attributes != "")
            {
                queryObject = JSON.parse("{" + attributes + "}");
                if(queryObject != null && queryObject.data != null)
                {
                    queryObject.data = JSON.parse("{" + this._parseQueryStringAsJSONString(queryObject.data, null) + "}");
                }
            }
        }
        return queryObject;
    },
  
    _parseQueryStringAsJSONString : function (queryString, avoidQueryItem) {
        var attributes = "";

        if (queryString != "") {
            var queryItems = queryString.split("&");
            for (var i in queryItems) {

                queryItems[i] = queryItems[i].split("=");
                
                if (queryItems[i][0].toLowerCase() != avoidQueryItem) // Avoid a specified query string item if required
                {
                    if(attributes != "")
                        attributes = attributes  + ",";

                    queryItems[i][1] = queryItems[i][1].replace(/\+/g, " ");
                    queryItems[i][1] = decodeURIComponent(queryItems[i][1]);

                    attributes  = attributes + "\"" + queryItems[i][0] + "\": \"" + queryItems[i][1] + "\"";
                } 
            }
        }
        return attributes;
    },
    __namespace: true
};

